﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLM.Domain.Model;
using DLM.Services.Interfaces;
using DLM.Web.ViewModel;
using Webdiyer.WebControls.Mvc;
using DLM.Web.MVC.Extensions;
using DLM.Infrastructure.Exception;

namespace DLM.Web.MVC.Controllers
{
    [UserAuthorize]
    public class FriendController : Controller
    {

        private IUserService userService;

        public FriendController(IUserService userService)
        {
            this.userService = userService;
        }

        public ActionResult List(int? requestid,int? friendid,int? historyid)
        {
            int pageIndex;
            int.TryParse(Request.QueryString["friendid"], out pageIndex);
            int hisid;
            int.TryParse(Request.QueryString["historyid"], out hisid);

            var requestList = this.userService.FriendRequest(User.Identity.Name, requestid ?? 0, 10);
            var friendList = this.userService.MyFriend(User.Identity.Name, pageIndex, 10);
            var histories = this.userService.FriendHistory(User.Identity.Name, hisid, 20);

            FriendPageView viewPage = new FriendPageView();
            viewPage.MyFriend = friendList;
            viewPage.MyRequest = requestList;
            viewPage.MyHistory = histories;
            if (Request.IsAjaxRequest())
            {
                if (friendid != null)
                {
                    return View("_MyFriend",viewPage.MyFriend);
                }
                if (requestid != null)
                {
                    return View("_MyFriendRequest", viewPage.MyRequest);
                }
                if (historyid != null)
                {
                    return View("_MyFriendHistory",viewPage.MyHistory);
                }
            }
            return View(viewPage);
        }

        [HttpGet]
        public ActionResult MyFriend(int?page)
        {
            var list = this.userService.MyFriend(User.Identity.Name, page ?? 1, 20);
            return View(list);
            
        }



        [HttpPost]
        public ActionResult Invite(int? id)
        {
            try
            {
                this.userService.AddFriend(User.Identity.Name, id ?? 0);
            }
            catch (FriendExistException fe)
            {
                //ModelState.AddModelError("", "您已经添加过该位好友");
                Response.StatusCode = 500;
                return Content(fe.Message);
            }

            return Content("");
        }

        [HttpGet]
        public ActionResult Agree()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Agree(int? id)
        {
            if (Request.IsAjaxRequest())
            {
                this.userService.ConfirmFriend(new Friend { FriendID = id ?? 0 });
                var list = this.userService.FriendRequest(User.Identity.Name, 0, 15);
                return View("_MyFriendRequest", list);
            }
            return Content("");
         
        }

        public ActionResult LoadMyFriend()
        {
            if (Request.IsAjaxRequest())
            {
                var list = this.userService.MyFriend(User.Identity.Name, 0, 20);
                return PartialView("_MyFriend", list);
            }
            return View();
        }

        


        public ActionResult Salutation(int? greetid,int? requestid,int? historyid)
        {
            int requestIndex;
            int.TryParse(Request.QueryString["requestid"], out requestIndex);
            int pageIndex;
            int.TryParse(Request.QueryString["historyid"], out pageIndex);
            SalutationPageView pageView = new SalutationPageView();
            var greets = this.userService.GetAgreedGreet(User.Identity.Name, greetid ?? 0, 20);
            pageView.Greets = greets;
            var requests = this.userService.GetMyGreet(User.Identity.Name, requestIndex, 20);
            pageView.Requests = requests;
            var histories = this.userService.GetGreetHistory(User.Identity.Name, pageIndex, 20);
            pageView.Histories = histories;
            if (Request.IsAjaxRequest())
            {
                if (greetid != null)
                {
                    return View("_MyGreets",pageView.Greets);
                }
                if (requestid != null)
                {
                    return View("_MyGreetRequest", pageView.Requests);
                }
                if (historyid != null)
                {
                    return View("_MyGreetHistories", pageView.Histories);
                }
            }
            return View(pageView);
        }

        [HttpPost]
        [UserAuthorize]
        public ActionResult Greet(FormCollection collection)
        {
            //if (greetID == null)
            //{
            //    ModelState.AddModelError("", "请选择要打招呼的用户");
            //    return View();
            //}

            try
            {
                int greetID = 0;
                if (!string.IsNullOrEmpty(collection["hdUserID"]))
                {
                    greetID = int.Parse(collection["hdUserID"].ToString());
                }
                int greetType = 0;
                if (!string.IsNullOrEmpty(collection["Salution.Type"]))
                {
                    greetType = int.Parse(collection["Salution.Type"].ToString());
                }
                string message = string.Empty;
                if (greetType == (int)GreetType.Phone)
                {
                    message = collection["Salution.Message"].ToString();
                }
                this.userService.AddGreet(User.Identity.Name, greetID, greetType, message);
            }
            catch (SalutationExistException se)
            {
                Response.StatusCode = 500;
                return Content(se.Message);

            }
            catch (BounsNotEnoughException bne)
            {

                Response.StatusCode = 500;
                return Content("您的积分目前不够打招呼");
            }

            return Content("");
        }

        [HttpPost]
        public ActionResult Confirm(int? id)
        {
            if (id == null)
            {
                ModelState.AddModelError("", "请选择招呼");
                return View();
            }
            try
            {
                this.userService.UpdateGreet(id ?? 0);
                var list = this.userService.GetMyGreet(User.Identity.Name, 0, 20);
                return View("_MyGreetRequest", list);
            }
            catch (BounsNotEnoughException bne)
            {

                Response.StatusCode = 500;
                return Content("您当前的积分不足");
            }
            return View();
        }

        [HttpPost]
        public ActionResult LoadMyGreet()
        {
            if (Request.IsAjaxRequest())
            {
                var list = this.userService.GetAgreedGreet(User.Identity.Name, 0, 20);
                return PartialView("_MyGreets", list);
            }
            return View();
        }

        [HttpPost]
        public ActionResult Remove(int? id)
        {
            if (id == null)
            {
                ModelState.AddModelError("", "请选择招呼");
                return View();
            }
            this.userService.RemoveGreet(id ?? 0);
            var list = this.userService.GetMyGreet(User.Identity.Name, 0, 20);
            return View("_MyGreetRequest",list);
        }


    }
}
